package com.heihei.config;

import com.heihei.properties.RedisConfigProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.Resource;

/**
 *  redis 配置
 *
 * @author duanhf
 * 2021年12月24日 14:16:54
 */
@Configuration
public class RedisConfiguration {

    @Resource
    private RedisConfigProperties redisConfigProperties;

    @Bean
    public JedisPool baseJedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(redisConfigProperties.getMaxIdle());
        config.setMaxTotal(redisConfigProperties.getMaxTotal());
        config.setMaxWaitMillis(redisConfigProperties.getMaxWaitMillis());
        config.setNumTestsPerEvictionRun(redisConfigProperties.getNumTestsPerEvictionRun());
        config.setTimeBetweenEvictionRunsMillis(redisConfigProperties.getTimeBetweenEvictionRunsMillis());
        config.setMinEvictableIdleTimeMillis(redisConfigProperties.getMinEvictableIdleTimeMillis());
        config.setSoftMinEvictableIdleTimeMillis(redisConfigProperties.getSoftMinEvictableIdleTimeMillis());
        config.setTestOnBorrow(redisConfigProperties.getTestOnBorrow());
        config.setTestWhileIdle(redisConfigProperties.getTestWhileIdle());
        config.setBlockWhenExhausted(redisConfigProperties.getBlockWhenExhausted());

        return new JedisPool(config, redisConfigProperties.getHost(), redisConfigProperties.getPort(), redisConfigProperties.getTimeout(), redisConfigProperties.getTimeout(),
                redisConfigProperties.getPassword(), redisConfigProperties.getDatabase(), redisConfigProperties.getClientName());

    }

}
